//Written by Richard Lewis aka Trixmoto (c) 2007

var x = y = thick = 0;

function drawForm(form) {
  //initialise
  if (!form.visi) {
    return "";
  }
  var arr = findpos(document.getElementById('form'));
  x = arr[0];
  y = arr[1]-20;

  //form
  var temp = "<div id='"+form.onam+"' class='uiform' style='";
  temp += "top:"+(y+form.top)+"px;";
  temp += " left:"+(x+form.left)+"px;";
  temp += " width:"+(form.widt-5)+"px;";
  
  switch(form.type) {
    case "frm":
      thick = 28;
      if (form.bord<2) {
        temp += " height:"+(form.heig-thick)+"px;";
        temp += " border-style:none; background-color:white;";
      } 
      else {
        if (form.bord>3) {
          thick = thick-6;
          temp += " height:"+(form.heig-6)+"px;";
        }
        else {
          temp += " height:"+form.heig+"px;";
        }
      }
      break;
    case "doc":
      temp += " height:"+form.heig+"px;";
      break;
    case "opt":
      temp += " height:"+form.heig+"px;";
      break;      
  }
  temp += "' title='"+form.hint+"' onmousedown='return startdrag(event)' onmousemove='return dodrag(event)' onmouseup='return stopdrag(event)'>";

  //titlebar  
  if (form.type=="frm" && form.bord>1) {
    temp += "<div class='title' style='width:";
    temp += (form.widt-4)+"px; height:"+(thick-2)+"px; z-index:1'>";
    temp += "<span style='position:relative; top:3px; left:8px'>";
    temp += "<img src='imgs/x.png' alt='Close' height=16 width=16 align=right>";
    if (form.bord<4) {
      temp += "<img src='imgs/mm.png' height="+thick+" width="+thick+" align=left>";  
      if (form.bord==2) {
        temp += "<img src='imgs/w.png' alt='Max' height=16 width=16 align=right>";
        temp += "<img src='imgs/v.png' alt='Min' height=16 width=16 align=right>";
      }
    }
    temp += "<b>"+form.capt+"</b></span></div>"; 
  }  
  
  //pointer
  temp += "<div id='pointer' class='pointer'><img src='imgs/sel.png'></div>";
  
  return temp+drawChildren(form,2)+"</div>";
}

function drawChildren(obj,lev) {
  var temp = "";
  var arr = obj.kids;
  for(var i=0;i<arr.length;i++) {
    switch(arr[i].type) {
      case "act":
        temp += drawActivex(obj,arr[i],lev);
        break;
      case "but":
        temp += drawButton(obj,arr[i],lev);
        break;        
      case "drp":
        temp += drawDropDown(obj,arr[i],lev);
        break;        
      case "edt":
        temp += drawEdit(obj,arr[i],lev);
        break;                  
      case "chk":
        temp += drawCheckBox(obj,arr[i],lev);     
        break;
      case "lbl":
        temp += drawLabel(obj,arr[i],lev);     
        break;        
      case "pan":
        temp += drawPanel(obj,arr[i],lev);     
        break;     
      case "rad":
        temp += drawRadioButton(obj,arr[i],lev);     
        break;        
      case "spn":
        temp += drawSpinEdit(obj,arr[i],lev);     
        break;   
      case "bar":
        temp += drawTrackBar(obj,arr[i],lev);     
        break;        
      case "tra":
        temp += drawTranspPanel(obj,arr[i],lev);     
        break;
    }  
  }
  return temp;
}

function drawActivex(par,act,lev) {
  if (!act.visi) {
    return "";
  }
  
  var thisthick = 0;
  if(par==MyForm) {
    thisthick = thick;
  }  
  var tp = act.top+thisthick;
  var lf = act.left;
  var wd = act.widt;
  var hg = act.heig;
  
  //alignment
  switch(act.alig) {
    case 1:
      tp = thisthick;
      lf = 0;
      wd = par.widt;
      break;
    case 2:
      tp = par.heig-act.heig-1;
      lf = 0;
      wd = par.widt;
      break;
    case 3:
      lf = 0;
      tp = thisthick;
      hg = par.heig-thisthick-1;
      break;
    case 4:
      lf = par.widt-act.widt;
      tp = thisthick;
      hg = par.heig-thisthick-1;
      break;
    case 5:
      tp = thisthick;
      lf = 0;
      wd = par.widt;
      hg = par.heig-thisthick-1;
  }
  
  //hint
  var title = ""
  if(act.hint!="") {
    title = " title='"+act.hint+"'";
  }
  
  var ret = "<div id='"+act.onam+"' class='uiactivex' style='width:"+(wd-1)+"px; height:"+hg+"px;";
  ret += " top:"+(tp-1)+"px; left:"+(lf-1)+"px; z-index:"+lev+";'"+title+" onmousedown='return startdrag(event)' onmousemove='return dodrag(event)' onmouseup='return stopdrag(event)'>";
  switch(act.comp) {
    case "Shell.Explorer":
      ret += "<img src='imgs/ie.png' alt='Shell.Explorer'>";
      break;
    default:
      ret += "<img src='imgs/un.png' alt='(Unknown)'>";
  }
  return ret+"</div>";
}

function drawButton(par,but,lev) {
  if (!but.visi) {
    return "";
  }
  
  var thisthick = 0;
  if(par==MyForm) {
    thisthick = thick;
  }
  var tp = but.top+thisthick;
  var lf = but.left;
  var wd = but.widt;
  var hg = but.heig;
  
  //alignment
  switch(but.alig) {
    case 1:
      tp = thisthick;
      lf = 0;
      wd = par.widt;
      break;
    case 2:
      tp = par.heig-but.heig-1;
      lf = 0;
      wd = par.widt;
      break;
    case 3:
      lf = 0;
      tp = thisthick;
      hg = par.heig-thisthick-1;
      break;
    case 4:
      lf = par.widt-but.widt;
      tp = thisthick;
      hg = par.heig-thisthick-1;
      break;
    case 5:
      tp = thisthick;
      lf = 0;
      wd = par.widt;
      hg = par.heig-thisthick-1;
  }
  
  //hint
  var title = ""
  if(but.hint!="") {
    title = " title='"+but.hint+"'";
  }
  
  //caption
  var caption = but.capt;
  var i = caption.indexOf("&");
  if(i>-1) {
    var pre = "";
    if(i>0) {
      pre = caption.substring(0,i);
    }
    var pst = "";
    if((i+2)<caption.length) {
      pst = caption.substring(i+2);
    }
    caption = pre+"<u>"+caption.substring(i+1,i+2)+"</u>"+pst;
  }
  
  var ret = "<div id='"+but.onam+"' class='uibutton' style='width:"+(wd-2)+"px; height:"+hg+"px;";
  ret += " top:"+(tp-1)+"px; left:"+lf+"px; z-index:"+lev+";'"+title+" onmousedown='return startdrag(event)' onmousemove='return dodrag(event)' onmouseup='return stopdrag(event)'>";
  ret += "<span style='position:relative; top:6px;'><center>"+caption+"</center></span>";
  return ret+"</div>";
}

function drawDropDown(par,drp,lev) {
  if (!drp.visi) {
    return "";
  }
  
  var thisthick = 0;
  if(par==MyForm) {
    thisthick = thick;
  }  
  var tp = drp.top+thisthick;
  var lf = drp.left;
  var wd = drp.widt;
  var hg = drp.heig;
  
  //alignment
  switch(drp.alig) {
    case 1:
      tp = thisthick;
      lf = 0;
      wd = par.widt+2;
      break;
    case 2:
      tp = par.heig-drp.heig+1;
      lf = 0;
      wd = par.widt+2;
      break;
    case 3:
      lf = 0;
      tp = thisthick;
      hg = par.heig-thisthick+1;
      break;
    case 4:
      lf = par.widt-drp.widt+2;
      tp = thisthick;
      hg = par.heig-thisthick+1;
      break;
    case 5:
      tp = thisthick;
      lf = 0;
      wd = par.widt+2;
      hg = par.heig-thisthick+1;
  }
  
  //hint
  var title = ""
  if(drp.hint!="") {
    title = " title='"+drp.hint+"'";
  }
  
  var ret = "<div id='"+drp.onam+"' class='uidropdown' style='width:"+(wd-2)+"px; height:"+hg+"px;";
  ret += " top:"+(tp-1)+"px; left:"+lf+"px; z-index:"+lev+";'"+title+" onmousedown='return startdrag(event)' onmousemove='return dodrag(event)' onmouseup='return stopdrag(event)'>";
  ret += "<span style='position:relative; top:3px; left:3px;'>";
  ret += "<img src='imgs/drp.png' alt='&#8595' height=19 width=19 align=right>";
  return ret+drp.text+"</span></div>";
}

function drawEdit(par,edt,lev) {
  if (!edt.visi) {
    return "";
  }
  
  var thisthick = 0;
  if(par==MyForm) {
    thisthick = thick;
  }
  var tp = edt.top+thisthick;
  var lf = edt.left;
  var wd = edt.widt;
  var hg = edt.heig;
  
  //alignment
  switch(edt.alig) {
    case 1:
      tp = thisthick;
      lf = 0;
      wd = par.widt+2;
      break;
    case 2:
      tp = par.heig-edt.heig+1;
      lf = 0;
      wd = par.widt+2;
      break;
    case 3:
      lf = 0;
      tp = thisthick;
      hg = par.heig-thisthick+1;
      break;
    case 4:
      lf = par.widt-edt.widt+2;
      tp = thisthick;
      hg = par.heig-thisthick+1;
      break;
    case 5:
      tp = thisthick;
      lf = 0;
      wd = par.widt+2;
      hg = par.heig-thisthick+1;
  }
  
  //hint
  var title = ""
  if(edt.hint!="") {
    title = " title='"+edt.hint+"'";
  }
  
  var ret = "<div id='"+edt.onam+"' class='uiedit' style='width:"+(wd-2)+"px; height:"+hg+"px;";
  ret += " top:"+(tp-1)+"px; left:"+lf+"px; z-index:"+lev+";'"+title+" onmousedown='return startdrag(event)' onmousemove='return dodrag(event)' onmouseup='return stopdrag(event)'>";
  ret += "<span style='position:relative; top:4px; left:3px;'>"+edt.text+"</span>";
  return ret+"</div>";
}

function drawCheckBox(par,chk,lev) {
  if (!chk.visi) {
    return "";
  }
  
  var thisthick = 0;
  if(par==MyForm) {
    thisthick = thick;
  }  
  var tp = chk.top+thisthick;
  var lf = chk.left;
  var wd = chk.widt;
  var hg = chk.heig;
  
  //alignment
  switch(chk.alig) {
    case 1:
      tp = thisthick;
      lf = 0;
      wd = par.widt+2;
      break;
    case 2:
      tp = par.heig-chk.heig+1;
      lf = 0;
      wd = par.widt+2;
      break;
    case 3:
      lf = 0;
      tp = thisthick;
      hg = par.heig-thisthick+1;
      break;
    case 4:
      lf = par.widt-chk.widt+2;
      tp = thisthick;
      hg = par.heig-thisthick+1;
      break;
    case 5:
      tp = thisthick;
      lf = 0;
      wd = par.widt+2;
      hg = par.heig-thisthick+1;
  }
  
  //hint
  var title = ""
  if(chk.hint!="") {
    title = " title='"+chk.hint+"'";
  }
  
  var ret = "<div id='"+chk.onam+"' class='uicheckbox' style='width:"+(wd-2)+"px; height:"+hg+"px;";
  ret += " top:"+(tp-1)+"px; left:"+lf+"px; z-index:"+lev+";'"+title+" onmousedown='return startdrag(event)' onmousemove='return dodrag(event)' onmouseup='return stopdrag(event)'>";
  ret += "<span style='position:relative; top:4px; left:3px;'>";
  if(chk.chkd) {
    ret += "<img src='imgs/chkd.png' alt='[*]' height=17 width=15 align=left>";
  }
  else {
    ret += "<img src='imgs/chk.png' alt='[ ]' height=17 width=15 align=left>";
  }
  return ret+chk.capt+"</span></div>";
}

function drawLabel(par,lbl,lev) {
  if (!lbl.visi) {
    return "";
  }
  
  var thisthick = 0;
  if(par==MyForm) {
    thisthick = thick;
  }  
  var tp = lbl.top+thisthick;
  var lf = lbl.left;
  var wd = lbl.widt;
  var hg = lbl.heig;
  var al = 2;
  var ta = "left";
  
  //autosize
  var caption = lbl.capt;
  if(lbl.auto) {
    var arr = caption.split("\\n");
    hg = 13*arr.length;
    wd = 0;
    for(var i=0;i<arr.length;i++) {
      var tw = 0; //!
      if(tw>wd) {
        wd = tw;
      }
    }
  }
  else {
    //caption alignment
    if(lbl.algn==1) {
      ta = "right";
    }
    if(lbl.algn==2) {
      ta = "center";
    }     
  }
  caption = caption.replace(/\\n/g,"<br>");
  
  //multiline
  if(!lbl.mult) {
    caption = caption.replace(/<br>/g,"</nobr><br><nobr>");
    caption = "<nobr>"+caption+"</nobr>";
  }   
  
  //alignment
  switch(lbl.alig) {
    case 1:
      tp = thisthick;
      lf = 0;
      wd = par.widt+2;
      break;
    case 2:
      tp = par.heig-lbl.heig+1;
      lf = 0;
      wd = par.widt+2;
      break;
    case 3:
      lf = 0;
      tp = thisthick;
      hg = par.heig-thisthick+1;
      break;
    case 4:
      lf = par.widt-lbl.widt+2;
      tp = thisthick;
      hg = par.heig-thisthick+1;
      break;
    case 5:
      tp = thisthick;
      lf = 0;
      wd = par.widt+2;
      hg = par.heig-thisthick+1;
  }
  
  //hint
  var title = ""
  if(lbl.hint!="") {
    title = " title='"+lbl.hint+"'";
  }
  
  var ret = "<div id='"+lbl.onam+"' class='uilabel' style='width:"+(wd-2)+"px; height:"+hg+"px;";
  ret += " top:"+(tp-1)+"px; left:"+lf+"px; z-index:"+lev+";'"+title+" onmousedown='return startdrag(event)' onmousemove='return dodrag(event)' onmouseup='return stopdrag(event)'>";
  ret += "<div style='position:relative; left:"+al+"px; width:"+(wd-4-al)+"px; text-align:"+ta+"'>";
  return ret+caption+"</div></div>";
}

function drawPanel(par,pan,lev) {
  if (!pan.visi) {
    return "";
  }

  var thisthick = 0;
  if(par==MyForm) {
    thisthick = thick;
  }  
  var tp = pan.top+thisthick;
  var lf = pan.left;
  var wd = pan.widt;
  var hg = pan.heig;   
  
  //alignment
  switch(pan.alig) {
    case 1:
      tp = thisthick;
      lf = 0;
      wd = par.widt+2;
      break;
    case 2:
      tp = par.heig-pan.heig+1;
      lf = 0;
      wd = par.widt+2;
      break;
    case 3:
      lf = 0;
      tp = thisthick;
      hg = par.heig-thisthick+1;
      break;
    case 4:
      lf = par.widt-pan.widt+2;
      tp = thisthick;
      hg = par.heig-thisthick+1;
      break;
    case 5:
      tp = thisthick;
      lf = 0;
      wd = par.widt+2;
      hg = par.heig-thisthick+1;
  }
  
  //hint
  var title = ""
  if(pan.hint!="") {
    title = " title='"+pan.hint+"'";
  }
  
  var ret = "<div id='"+pan.onam+"' class='uipanel' style='width:"+(wd-2)+"px; height:"+hg+"px;";
  ret += " top:"+(tp-1)+"px; left:"+lf+"px; z-index:"+lev+";'"+title+" onmousedown='return startdrag(event)' onmousemove='return dodrag(event)' onmouseup='return stopdrag(event)'>";
  return ret+drawChildren(pan,lev+1)+"</div>";
}

function drawRadioButton(par,rad,lev) {
  if (!rad.visi) {
    return "";
  }
  
  var thisthick = 0;
  if(par==MyForm) {
    thisthick = thick;
  }  
  var tp = rad.top+thisthick;
  var lf = rad.left;
  var wd = rad.widt;
  var hg = rad.heig;
  
  //alignment
  switch(rad.alig) {
    case 1:
      tp = thisthick;
      lf = 0;
      wd = par.widt+2;
      break;
    case 2:
      tp = par.heig-rad.heig+1;
      lf = 0;
      wd = par.widt+2;
      break;
    case 3:
      lf = 0;
      tp = thisthick;
      hg = par.heig-thisthick+1;
      break;
    case 4:
      lf = par.widt-rad.widt+2;
      tp = thisthick;
      hg = par.heig-thisthick+1;
      break;
    case 5:
      tp = thisthick;
      lf = 0;
      wd = par.widt+2;
      hg = par.heig-thisthick+1;
  }
  
  //hint
  var title = ""
  if(rad.hint!="") {
    title = " title='"+rad.hint+"'";
  }
  
  var ret = "<div id='"+rad.onam+"' class='uiradiobutton' style='width:"+(wd-2)+"px; height:"+hg+"px;";
  ret += " top:"+(tp-1)+"px; left:"+lf+"px; z-index:"+lev+";'"+title+" onmousedown='return startdrag(event)' onmousemove='return dodrag(event)' onmouseup='return stopdrag(event)'>";
  ret += "<span style='position:relative; top:4px; left:3px;'>";
  if(rad.chkd) {
    ret += "<img src='imgs/radd.png' alt='(*)' height=17 width=15 align=left>";
  }
  else {
    ret += "<img src='imgs/rad.png' alt='( )' height=17 width=15 align=left>";
  }
  return ret+rad.capt+"</span></div>";
}

function drawSpinEdit(par,spn,lev) {
  if (!spn.visi) {
    return "";
  }
  
  var thisthick = 0;
  if(par==MyForm) {
    thisthick = thick;
  }  
  var tp = spn.top+thisthick;
  var lf = spn.left;
  var wd = spn.widt;
  var hg = spn.heig;
  
  //alignment
  switch(spn.alig) {
    case 1:
      tp = thisthick;
      lf = 0;
      wd = par.widt+2;
      break;
    case 2:
      tp = par.heig-spn.heig+1;
      lf = 0;
      wd = par.widt+2;
      break;
    case 3:
      lf = 0;
      tp = thisthick;
      hg = par.heig-thisthick+1;
      break;
    case 4:
      lf = par.widt-spn.widt+2;
      tp = thisthick;
      hg = par.heig-thisthick+1;
      break;
    case 5:
      tp = thisthick;
      lf = 0;
      wd = par.widt+2;
      hg = par.heig-thisthick+1;
  }
  
  //hint
  var title = ""
  if(spn.hint!="") {
    title = " title='"+spn.hint+"'";
  }
  
  var ret = "<div id='"+spn.onam+"' class='uispinedit' style='width:"+(wd-2)+"px; height:"+hg+"px;";
  ret += " top:"+(tp-1)+"px; left:"+lf+"px; z-index:"+lev+";'"+title+" onmousedown='return startdrag(event)' onmousemove='return dodrag(event)' onmouseup='return stopdrag(event)'>";
  ret += "<span style='position:relative; top:3px; left:3px;'>";
  ret += "<img src='imgs/spn.png' alt='&#177' height=19 width=19 align=right>";
  return ret+spn.valu+"</span></div>";
}

function drawTrackBar(par,bar,lev) {
  if (!bar.visi) {
    return "";
  }
  
  var thisthick = 0;
  if(par==MyForm) {
    thisthick = thick;
  }  
  var tp = bar.top+thisthick;
  var lf = bar.left;
  var wd = bar.widt;
  var hg = bar.heig;
  
  //alignment
  switch(bar.alig) {
    case 1:
      tp = thisthick;
      lf = 0;
      wd = par.widt+2;
      break;
    case 2:
      tp = par.heig-bar.heig+1;
      lf = 0;
      wd = par.widt+2;
      break;
    case 3:
      lf = 0;
      tp = thisthick;
      hg = par.heig-thisthick+1;
      break;
    case 4:
      lf = par.widt-bar.widt+2;
      tp = thisthick;
      hg = par.heig-thisthick+1;
      break;
    case 5:
      tp = thisthick;
      lf = 0;
      wd = par.widt+2;
      hg = par.heig-thisthick+1;
  }
  
  //hint
  var title = ""
  if(bar.hint!="") {
    title = " title='"+bar.hint+"'";
  }
  
  //calculate position
  var bl = 10;
  var bw = wd-(bl*2);
  var tl = parseInt((bw*bar.valu)/(bar.maxv-bar.minv))+bl-5;
  
  var ret = "<div id='"+bar.onam+"' class='uitrackbar' style='width:"+(wd-2)+"px; height:"+hg+"px;";
  ret += " top:"+(tp-1)+"px; left:"+lf+"px; z-index:"+lev+";'"+title+" onmousedown='return startdrag(event)' onmousemove='return dodrag(event)' onmouseup='return stopdrag(event)'>";
  ret += "<span style='position:absolute; top:10px; left:"+bl+"px;'>";
  ret += "<img src='imgs/bar.png' alt='----------' height=6 width="+bw+"></span>";
  ret += "<span style='position:absolute; top:7px; left:"+tl+"px;'>";
  ret += "<img src='imgs/trk.png' alt='|' height=13 width=11></span>";  
  
  //!markings
  
  return ret+"</div>";
}

function drawTranspPanel(par,tra,lev) {
  if (!tra.visi) {
    return "";
  }

  var thisthick = 0;
  if(par==MyForm) {
    thisthick = thick;
  }  
  var tp = tra.top+thisthick;
  var lf = tra.left;
  var wd = tra.widt;
  var hg = tra.heig;   
  
  //alignment
  switch(tra.alig) {
    case 1:
      tp = thisthick;
      lf = 0;
      wd = par.widt+2;
      break;
    case 2:
      tp = par.heig-tra.heig+1;
      lf = 0;
      wd = par.widt+2;
      break;
    case 3:
      lf = 0;
      tp = thisthick;
      hg = par.heig-thisthick+1;
      break;
    case 4:
      lf = par.widt-tra.widt+2;
      tp = thisthick;
      hg = par.heig-thisthick+1;
      break;
    case 5:
      tp = thisthick;
      lf = 0;
      wd = par.widt+2;
      hg = par.heig-thisthick+1;
  }
  
  //hint
  var title = ""
  if(tra.hint!="") {
    title = " title='"+tra.hint+"'";
  }
  
  var ret = "<div id='"+tra.onam+"' class='uitransppanel' style='width:"+(wd-2)+"px; height:"+hg+"px;";
  ret += " top:"+(tp-1)+"px; left:"+lf+"px; z-index:"+lev+";'"+title+" onmousedown='return startdrag(event)' onmousemove='return dodrag(event)' onmouseup='return stopdrag(event)'>";
  return ret+drawChildren(tra,lev+1)+"</div>";
}
